package com.sz.virtualbank.web.controller.bankcard;

import com.sz.virtualbank.common.annotation.LoginRequired;
import com.sz.virtualbank.common.result.CommonResponse;
import com.sz.virtualbank.dao.entity.BankCard;
import com.sz.virtualbank.dao.entity.User;
import com.sz.virtualbank.service.bankcard.BankCardService;
import com.sz.virtualbank.service.user.UserService;
import com.sz.virtualbank.web.vo.bankcard.BankCardAddReqVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 银行卡相关接口
 * 
 * @author Claude
 */
@RestController
@RequestMapping("/api/bankcard")
@Slf4j
@Api(tags = "银行卡接口")
public class BankCardController {

    @Autowired
    private BankCardService bankCardService;
    
    @Autowired
    private UserService userService;
    
    /**
     * 查询银行卡列表
     */
    @GetMapping("/list")
    @ApiOperation("查询银行卡列表")
    @LoginRequired
    public CommonResponse<List<BankCard>> getBankCardList(HttpServletRequest request) {
        String token = request.getHeader("Authorization").replace("Bearer ", "");
        User user = userService.getCurrentUser(token);
        
        List<BankCard> bankCards = bankCardService.getBankCardList(user.getId());
        return CommonResponse.success(bankCards);
    }
    
    /**
     * 添加银行卡
     */
    @PostMapping("/add")
    @ApiOperation("添加银行卡")
    @LoginRequired
    public CommonResponse<BankCard> addBankCard(@RequestBody @Valid BankCardAddReqVO reqVO,
                                               HttpServletRequest request) {
        String token = request.getHeader("Authorization").replace("Bearer ", "");
        User user = userService.getCurrentUser(token);
        
        BankCard bankCard = new BankCard();
        BeanUtils.copyProperties(reqVO, bankCard);
        bankCard.setUserId(user.getId());
        bankCard.setCreateTime(LocalDateTime.now());
        bankCard.setUpdateTime(LocalDateTime.now());
        
        BankCard result = bankCardService.addBankCard(bankCard);
        return CommonResponse.success(result);
    }
    
    /**
     * 查询银行卡详情
     */
    @GetMapping("/{id}")
    @ApiOperation("查询银行卡详情")
    @LoginRequired
    public CommonResponse<BankCard> getBankCardDetail(@PathVariable Long id, 
                                                     HttpServletRequest request) {
        String token = request.getHeader("Authorization").replace("Bearer ", "");
        User user = userService.getCurrentUser(token);
        
        BankCard bankCard = bankCardService.getBankCardDetail(id, user.getId());
        return CommonResponse.success(bankCard);
    }
    
    /**
     * 设置默认银行卡
     */
    @PutMapping("/default/{id}")
    @ApiOperation("设置默认银行卡")
    @LoginRequired
    public CommonResponse<Boolean> setDefaultCard(@PathVariable Long id, 
                                                HttpServletRequest request) {
        String token = request.getHeader("Authorization").replace("Bearer ", "");
        User user = userService.getCurrentUser(token);
        
        boolean result = bankCardService.setDefaultCard(id, user.getId());
        return CommonResponse.success(result);
    }
    
    /**
     * 删除银行卡
     */
    @DeleteMapping("/{id}")
    @ApiOperation("删除银行卡")
    @LoginRequired
    public CommonResponse<Boolean> deleteBankCard(@PathVariable Long id, 
                                                 HttpServletRequest request) {
        String token = request.getHeader("Authorization").replace("Bearer ", "");
        User user = userService.getCurrentUser(token);
        
        boolean result = bankCardService.deleteBankCard(id, user.getId());
        return CommonResponse.success(result);
    }
}
